<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\goods;

use app\adminapi\controller\Base;
use app\common\model\GoodsComment;
use app\common\model\Goods;

/**
 * Goods comment management
 */
class Comment extends Base
{
    /**
     * Comment list
     */
    public function lists()
    {
        $params = $this->request->params([
            ['goods_id', ''],
            ['keyword', ''],
            ['status', ''],
            ['user_id', ''],
            ['parent_id', ''],
        ]);

        $list = GoodsComment::with(['goods' => function ($query) {
            $query->field('id,name');
        },
            'user'  => function ($query) {
                $query->field('id,username,nickname,avatar');
            }
        ])
            ->withSearch($params[0], $params[1])
            ->order('id', 'desc')
            ->paginate($this->limit);

        return $this->success('', [
            'list'  => $list->items(),
            'total' => $list->total()
        ]);
    }

    /**
     * Audit comment
     */
    public function audit()
    {
        $id     = $this->request->post('id');
        $status = $this->request->post('status');

        if (
            !$id || !in_array($status, [
                GoodsComment::STATUS_APPROVED,
                GoodsComment::STATUS_REJECTED
            ])
        ) {
            return $this->error(trans('parameter_error'));
        }

        $comment = GoodsComment::find($id);
        if (!$comment) {
            return $this->error(trans('comment_not_exists'));
        }

        $comment->save(['status' => $status]);

        // Update goods comment count
        if ($status == GoodsComment::STATUS_APPROVED) {
            $goods = Goods::find($comment->goods_id);
            if ($goods) {
                $goods->setInc('comments_count');
            }
        }

        return $this->success();
    }

    /**
     * Delete comment
     */
    public function delete()
    {
        $id = $this->request->post('id');
        if (!$id) {
            return $this->error(trans('parameter_error'));
        }

        $comment = GoodsComment::find($id);
        if (!$comment) {
            return $this->error(trans('comment_not_exists'));
        }

        // If it's an approved comment, decrease goods comment count when deleting
        if ($comment->status == GoodsComment::STATUS_APPROVED) {
            $goods = Goods::find($comment->goods_id);
            if ($goods && $goods->comments_count > 0) {
                $goods->setDec('comments_count');
            }
        }

        // Delete comment and its replies
        GoodsComment::destroy(function ($query) use ($id) {
            $query->where('id', $id)->whereOr('parent_id', $id);
        });

        return $this->success();
    }
}